<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="Hexo Theme Keep">
    <meta name="description" content="Hexo Theme Keep">
    <meta name="author" content="cm_westwood">
    
    <title>
        
            第7周 |
        
        西木同学的代码训练 2022
    </title>
    
<link rel="stylesheet" href="/code_training_2022/css/style.css">

    <link rel="shortcut icon" href="">
    
<link rel="stylesheet" href="/code_training_2022/css/font-awesome.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"cm_westwood.gitee.io","root":"/code_training_2022/","language":"en","path":"search.json"};
    KEEP.theme_config = {"toc":{"enable":true,"number":true,"expand_all":true,"init_open":true},"style":{"primary_color":"#0066CC","avatar":"","favicon":"","article_img_align":"left","left_side_width":"260px","content_max_width":"920px","hover":{"shadow":false,"scale":false},"first_screen":{"enable":true,"background_img":"https://xpoet.cn/images/bg.svg","description":"Keep writing and Keep loving."},"scroll":{"progress_bar":{"enable":true},"percent":{"enable":true}}},"local_search":{"enable":true,"preload":false},"code_copy":{"enable":true,"style":"default"},"pjax":{"enable":false},"lazyload":{"enable":true},"version":"3.4.5"};
    KEEP.language_ago = {"second":"%s seconds ago","minute":"%s minutes ago","hour":"%s hours ago","day":"%s days ago","week":"%s weeks ago","month":"%s months ago","year":"%s years ago"};
  </script>
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
<div class="progress-bar-container">
    
        <span class="scroll-progress-bar"></span>
    

    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            <header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                西木同学的代码训练 2022
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/code_training_2022/"
                            >
                                HOME
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/code_training_2022/archives"
                            >
                                ARCHIVES
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/code_training_2022/tags"
                            >
                                TAGS
                            </a>
                        </li>
                    
                    
                        <li class="menu-item search search-popup-trigger">
                            <i class="fas fa-search"></i>
                        </li>
                    
                </ul>
            </div>
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fas fa-search"></i></div>
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/code_training_2022/">HOME</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/code_training_2022/archives">ARCHIVES</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/code_training_2022/tags">TAGS</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="article-content-container">

        <div class="article-title">
            <span class="title-hover-animation">第7周</span>
        </div>

        
            <div class="article-header">
                <div class="avatar">
                    <img src="">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">cm_westwood</span>
                        
                            <span class="author-label">Lv2</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fas fa-edit"></i>&nbsp;
        <span class="pc">2022-11-25 23:36:46</span>
        <span class="mobile">2022-11-25 23:36</span>
    </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fas fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/code_training_2022/tags/Python/">Python</a>&nbsp;
                    </li>
                
                    <li>
                        | <a href="/code_training_2022/tags/Jupyter-Notebook/">Jupyter Notebook</a>&nbsp;
                    </li>
                
                    <li>
                        | <a href="/code_training_2022/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
    
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <blockquote>
<p>最近这几个月我都太懒了啊，没有坚持下来每周更新，接下来可要好好加油啦</p>
<p>本周来介绍几个好用的 python 库吧</p>
</blockquote>
<p>Python - memory_profiler 内存分析工具；Jupyter notebook 中的进度条；Python 别再用 format 拼链接啦 - 用 urlencode 分分钟字典传参；Python 别再用 format 拼数据库查询语句啦 - pandas.io.sql；Python 中的多任务以及序列化 - Joblib</p>
<span id="more"></span>

<h1 id="Python-memory-profiler-内存分析工具"><a href="#Python-memory-profiler-内存分析工具" class="headerlink" title="Python - memory_profiler 内存分析工具"></a>Python - memory_profiler 内存分析工具</h1><p>我曾经用这个内存分析工具 <a class="link"   target="_blank" rel="noopener" href="https://pypi.org/project/memory-profiler/" >https://pypi.org/project/memory-profiler/<i class="fas fa-external-link-alt"></i></a> 找到了一行 python 内存泄露的代码</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://www.aiuai.cn/aifarm1520.html" >Python - memory_profiler 内存分析工具<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<h1 id="Jupyter-notebook-中的进度条"><a href="#Jupyter-notebook-中的进度条" class="headerlink" title="Jupyter notebook 中的进度条"></a>Jupyter notebook 中的进度条</h1><p>回想当初傻傻的我记得用 tqdm 打印进度条 <a class="link"   target="_blank" rel="noopener" href="https://pypi.org/project/tqdm/%EF%BC%8C%E5%8D%B4%E5%BF%98%E4%BA%86%E4%BD%BF%E7%94%A8" >https://pypi.org/project/tqdm/，却忘了使用<i class="fas fa-external-link-alt"></i></a> <code>from tqdm.notebook import tqdm</code> 导入 notebook 专用的进度条</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://stackoverflow.com/questions/42212810/tqdm-in-jupyter-notebook-prints-new-progress-bars-repeatedly" >tqdm in Jupyter Notebook prints new progress bars repeatedly<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<h1 id="Python-别再用-format-拼链接啦-用-urlencode-分分钟字典传参"><a href="#Python-别再用-format-拼链接啦-用-urlencode-分分钟字典传参" class="headerlink" title="Python 别再用 format 拼链接啦 - 用 urlencode 分分钟字典传参"></a>Python 别再用 format 拼链接啦 - 用 urlencode 分分钟字典传参</h1><p>python 处理字符串固然简单，但是构造一个 http 链接有专门的库去干这件事情，format 可不会帮你处理链接中不能存在的特殊符号</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://stackoverflow.com/questions/65199173/how-to-format-string-in-url-by-python" >How to format string in url by python? - Stack Overflow<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<h1 id="Python-别再用-format-拼数据库查询语句啦-pandas-io-sql"><a href="#Python-别再用-format-拼数据库查询语句啦-pandas-io-sql" class="headerlink" title="Python 别再用 format 拼数据库查询语句啦 - pandas.io.sql"></a>Python 别再用 format 拼数据库查询语句啦 - pandas.io.sql</h1><p>python 如何构造 sql 语句，这个 bing 百度一抓就是一大把用 format 去构造的，同样的，第一是数据库也有自己的特殊符号，如果存储的值里面有特殊符号存在，拼接出来的数据库语句就是有问题的，第二是这样就会导致 SQL 注入的问题，我看了源码啊，sql 最底层是通过字节流传输的查询语句，所以肯定不是在你 utf8 编码下能够处理完这些特殊符号的。</p>
<p>比如说这两个数据库的传参方式就很不一样：</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries" >https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries<i class="fas fa-external-link-alt"></i></a></p>
<p><a class="link"   target="_blank" rel="noopener" href="https://clickhouse-driver.readthedocs.io/en/latest/quickstart.html#selecting-data" >https://clickhouse-driver.readthedocs.io/en/latest/quickstart.html#selecting-data<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<p>那么应对不同的数据库，有没有一套通用的方法呢，这里是有的，<a class="link"   target="_blank" rel="noopener" href="https://docs.sqlalchemy.org/en/14/#" >SQLAlchemy 1.4 Documentation<i class="fas fa-external-link-alt"></i></a> 就是一套通用的数据库连接范式，结合 <a class="link"   target="_blank" rel="noopener" href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html" >pandas.io.sql<i class="fas fa-external-link-alt"></i></a> 可以达到和 pandas 很好的兼容，具体的可以去看这些人给的案例：</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://www.cnblogs.com/zxg-1997/p/10700692.html" >使用pandas操作MySQL数据库<i class="fas fa-external-link-alt"></i></a></p>
<p><a class="link"   target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/352615110" >Python 数据处理（二十二）—— SQL 查询<i class="fas fa-external-link-alt"></i></a></p>
<p><a class="link"   target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_44166997/article/details/102426996" >使用pandas获取PostgreSQL数据库中的数据<i class="fas fa-external-link-alt"></i></a></p>
<p><a class="link"   target="_blank" rel="noopener" href="https://github.com/pandas-dev/pandas/issues/7010" >read_sql fails with psycopg2 connection<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<h1 id="Python-中的多任务以及序列化-Joblib"><a href="#Python-中的多任务以及序列化-Joblib" class="headerlink" title="Python 中的多任务以及序列化 - Joblib"></a>Python 中的多任务以及序列化 - Joblib</h1><p><a class="link"   target="_blank" rel="noopener" href="https://joblib.readthedocs.io/en/latest/" >Joblib: running Python functions as pipeline jobs<i class="fas fa-external-link-alt"></i></a> 完美的解决 python 中繁琐的开启多线程多进程的写法，函数式编程，简简单单就能和现有的代码相结合，其自带的 Memory 装饰器，完美的解决了之前提到的 LRU 只能将数据缓存到内存的问题，能够自动的序列化 python 中的数据结构或者类到硬盘上，下次函数调用的时候，如果有，则读取硬盘上的缓存。</p>

        </div>

        

        
            <ul class="post-tags-box">
                
                    <li class="tag-item">
                        <a href="/code_training_2022/tags/Python/">#Python</a>&nbsp;
                    </li>
                
                    <li class="tag-item">
                        <a href="/code_training_2022/tags/Jupyter-Notebook/">#Jupyter Notebook</a>&nbsp;
                    </li>
                
                    <li class="tag-item">
                        <a href="/code_training_2022/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/">#数据库</a>&nbsp;
                    </li>
                
            </ul>
        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                           rel="prev"
                           href="/code_training_2022/2022/11/26/%E7%AC%AC8%E5%91%A8/"
                        >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                            <span class="title flex-center">
                                <span class="post-nav-title-item">第8周</span>
                                <span class="post-nav-item">Prev posts</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                           rel="next"
                           href="/code_training_2022/2022/08/20/%E7%AC%AC6%E5%91%A8/"
                        >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">第6周</span>
                                <span class="post-nav-item">Next posts</span>
                            </span>
                            <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        

        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
              <span>2022</span>
              -
            
            2023&nbsp;<i class="fas fa-heart icon-animate"></i>&nbsp;<a href="/">cm_westwood</a>
        </div>
        
        <div class="theme-info info-item">
            Powered by <a target="_blank" href="https://hexo.io">Hexo</a>&nbsp;|&nbsp;Theme&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.4.5</a>
        </div>
        
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        
            <li class="tools-item page-aside-toggle">
                <i class="fas fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-expand-width flex-center">
            <i class="fas fa-arrows-alt-h"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
    </ul>
</div>

    </div>

    
        <aside class="page-aside">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Python-memory-profiler-%E5%86%85%E5%AD%98%E5%88%86%E6%9E%90%E5%B7%A5%E5%85%B7"><span class="nav-number">1.</span> <span class="nav-text">Python - memory_profiler 内存分析工具</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Jupyter-notebook-%E4%B8%AD%E7%9A%84%E8%BF%9B%E5%BA%A6%E6%9D%A1"><span class="nav-number">2.</span> <span class="nav-text">Jupyter notebook 中的进度条</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Python-%E5%88%AB%E5%86%8D%E7%94%A8-format-%E6%8B%BC%E9%93%BE%E6%8E%A5%E5%95%A6-%E7%94%A8-urlencode-%E5%88%86%E5%88%86%E9%92%9F%E5%AD%97%E5%85%B8%E4%BC%A0%E5%8F%82"><span class="nav-number">3.</span> <span class="nav-text">Python 别再用 format 拼链接啦 - 用 urlencode 分分钟字典传参</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Python-%E5%88%AB%E5%86%8D%E7%94%A8-format-%E6%8B%BC%E6%95%B0%E6%8D%AE%E5%BA%93%E6%9F%A5%E8%AF%A2%E8%AF%AD%E5%8F%A5%E5%95%A6-pandas-io-sql"><span class="nav-number">4.</span> <span class="nav-text">Python 别再用 format 拼数据库查询语句啦 - pandas.io.sql</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Python-%E4%B8%AD%E7%9A%84%E5%A4%9A%E4%BB%BB%E5%8A%A1%E4%BB%A5%E5%8F%8A%E5%BA%8F%E5%88%97%E5%8C%96-Joblib"><span class="nav-number">5.</span> <span class="nav-text">Python 中的多任务以及序列化 - Joblib</span></a></li></ol>
    </div>
</div>
        </aside>
    

    <div class="image-viewer-container">
    <img src="">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fas fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="Search..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="popup-btn-close">
                <i class="fas fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fas fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>




<script src="/code_training_2022/js/utils.js"></script>

<script src="/code_training_2022/js/main.js"></script>

<script src="/code_training_2022/js/header-shrink.js"></script>

<script src="/code_training_2022/js/back2top.js"></script>

<script src="/code_training_2022/js/dark-light-toggle.js"></script>



    
<script src="/code_training_2022/js/local-search.js"></script>




    
<script src="/code_training_2022/js/code-copy.js"></script>




    
<script src="/code_training_2022/js/lazyload.js"></script>



<div class="post-scripts">
    
        
<script src="/code_training_2022/js/left-side-toggle.js"></script>

<script src="/code_training_2022/js/libs/anime.min.js"></script>

<script src="/code_training_2022/js/toc.js"></script>

    
</div>



</body>
</html>
